package cn.laixueit.service;

import cn.laixueit.client.GoodsClient;
import cn.laixueit.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class GoodsService {

    @Autowired
    private GoodsClient goodsClient;


    public Map<String, Object> loadData(Long spuId) {
        HashMap<String, Object> map = new HashMap<>();
        Spu spu = Optional.ofNullable(goodsClient.getSpuById(spuId).getBody()).orElse(new Spu());

        SpuDetail spuDetail = goodsClient.spuDetail(spuId);

        List<Sku> skus = goodsClient.skuListBySpuId(spuId);

        List<Category> categoryList = goodsClient.findCnameByList(Arrays.asList(spu.getCid1(), spu.getCid2(), spu.getCid3()));

        Brand brand = Optional.ofNullable(goodsClient.getBrandById(spu.getBrandId())).orElse(new Brand());

        List<SpecGroup> specGroupList = goodsClient.groupsByCid(spu.getCid3());

        List<SpecParam> specParamList = goodsClient.specParamsByGid(null, spu.getCid3(), null, false);

        HashMap<Long, String> paramMap = new HashMap<>();
        specParamList.forEach(param->{
            paramMap.put(param.getId(),param.getName());
        });

        map.put("spu",spu);
        map.put("spuDetail",spuDetail);
        map.put("categoryList",categoryList);
        map.put("brand",brand);
        map.put("skus",skus);
        map.put("specGroupList",specGroupList);
        map.put("paramMap",paramMap);

        return map;
    }
}
